-- AirshipConsole
-- Created by chendh Oct/08/2016
-- 飞艇控制台

-- 初始化
function initAirshipConsole(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            local data = ME.user.dbase:query("dungeon_sc_data") or {};
            local shipId = data["ship_id"] or ME.user.dbase:query("active_airship");
            local shipName = NewAirShipM.query(shipId, "name");

            local function viewFunc()
                -- 召唤飞艇
                cclog(G.."飞艇控制台召唤飞艇\n"..W);

                local isAllDead = true;
                local grids = DungeonM.getCurrentDungeon();
                for _, grid in pairs(grids) do
                    -- 没死的怪物才需要处理
                    if  grid:isMonster() and
                        not grid.monster:isDead() then
                        isAllDead = false;
                        break;
                    end
                end
                -- 如果当前场上已经没有怪物了，那么出现提示语，并且直接返回
                if isAllDead then
                    alert(getLocStr("no_monster"));
                    return;
                end

                -- "你向飞艇发出信号！"
                alert(getLocStr("airship_console_tip"));

                local function callBack()
                    -- 执行飞艇控制台操作
                    local pos = self.gridData:getPos();
                    DungeonSkyCombatM.airshipConsole(pos);

                    -- 飞艇控制台淡出消失
                    playBuildingDisappear(self);
                end

                -- 弹出对话
                startDialogueTemplate("airship_console", callBack,
                    {["ship_name"] = shipName, ["area_name"] = DungeonAreaM.query(1004,"name")});
            end

            -- 打开查看建筑
            self:openBuildingView(viewFunc, nil, string.format(buildingInfo["dialog"], shipName));
        end
    end

    -- 建筑图标
    self.item:setPositionY(25);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);

    self.icon:setScale(1);

    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
